package com.app.ui.activity.account;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.app.config.entity.ImageEntity;
import com.app.net.res.user.User;
import com.app.ui.activity.base.PhotoOptionActivity;
import com.app.ui.event.UserEvent;
import com.app.ui.pager.home.HomeMePager;
import com.app.ui.view.images.ImageLoadingView;
import com.app.utiles.image.ImageLoadingUtile;
import com.app.utiles.other.ActivityUtile;
import com.app.utiles.other.DLog;
import com.app.utiles.other.ToastUtile;
import com.wxj.zx.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;


/**
 * 个人数据修改
 * Created by Administrator on 2016/2/15.
 */
public class UserUpdateActivity extends PhotoOptionActivity {


    @Bind(R.id.pat_head_iv)
    ImageLoadingView patHeadIv;
    @Bind(R.id.user_name_tv)
    TextView userNameTv;
    @Bind(R.id.user_phone_tv)
    TextView userPhoneTv;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_update);
        setBarColor();
        ButterKnife.bind(this);
        initSeteleView();
        setBarBack();
        setBarTvText(1, "个人资料");
        user = baseApplication.getUser();
        userNameTv.setText(user.getUserNickName());
        userPhoneTv.setText(user.getMobilePhoneNumber());
        ImageLoadingUtile.loadingCircle(this, user.userHeadUrl,
                R.mipmap.default_head_pat_man, patHeadIv.getImageView());
        EventBus.getDefault().register(this);
    }

    @OnClick({R.id.pat_head_iv, R.id.user_name_tv, R.id.user_password_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pat_head_iv:
                //修改头像
                showView();
                break;
            case R.id.user_name_tv:
                //修改姓名
                ActivityUtile.startActivityCommon(UserNameActivity.class);
                break;
            case R.id.user_password_tv:
                //修改密码
                ActivityUtile.startActivityCommon(UserPasswordActivity.class);
                break;
        }
    }


    @Override
    protected void getImage(List<ImageEntity> images) {
        ImageEntity image = images.get(0);
        String imagePath = image.imagePathSource;
        ImageLoadingUtile.loadingCircle(this, imagePath,
                R.mipmap.default_head_pat_man, patHeadIv.getImageView());
        patHeadIv.setUpLodingShow();
        BmobFile bmobFile = new BmobFile(new File(imagePath));
        bmobFile.upload(this, new UploadListener(bmobFile));
    }

    //图片上传监听
    class UploadListener extends UploadFileListener {
        private BmobFile bmobFile;

        public UploadListener(BmobFile bmobFile) {
            this.bmobFile = bmobFile;
        }


        @Override
        public void onSuccess() {
            String url = bmobFile.getFileUrl(UserUpdateActivity.this);
            user.userHeadUrl = url;
            DLog.e("头像", "++" + url);
            user.update(UserUpdateActivity.this, new UserUpdateListener());
        }

        @Override
        public void onFailure(int i, String s) {
            patHeadIv.setUpLodingComplete();
            ToastUtile.showToast("头像上传失败");
        }
    }

    //保存个人信息监听
    class UserUpdateListener extends UpdateListener {
        @Override
        public void onSuccess() {
            patHeadIv.setUpLodingComplete();
            baseApplication.setUser(user);
            ToastUtile.showToast("修改成功");
            UserEvent userEvent = new UserEvent();
            userEvent.setClsName(HomeMePager.class);
            EventBus.getDefault().post(userEvent);
        }

        @Override
        public void onFailure(int i, String s) {
            patHeadIv.setUpLodingComplete();
            ToastUtile.showToast("修改失败");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnBack(UserEvent userEvent) {
        if (!userEvent.toCompareTag(this)) {
            return;
        }
        User user = baseApplication.getUser();
        String userNickName = user.getUserNickName();
        userNameTv.setText(userNickName);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


}
